<?php


namespace App\Services;


use Hyperf\Di\Annotation\Inject;
use Hyperf\Guzzle\ClientFactory;

class HttpService
{
    /**
     * @Inject(lazy=true)
     * @var ClientFactory
     */
    private $clientFactory;

    public function post(string $url, array $header, array $body, $timeout = 2): string
    {
        $client = $this->clientFactory->create([]);
        $response = $client->post($url, [
            'headers' => $header,
            'form_params' => $body,
            'timeout' => $timeout,
        ]);
        return $response->getBody()->getContents();
    }

    public function postJson(string $url, array $header, array $body, $timeout = 1): string
    {
        $client = $this->clientFactory->create([]);
        $response = $client->post($url, [
            'headers' => $header,
            'json' => $body,
            'timeout' => $timeout,
        ]);
        return $response->getBody()->getContents();
    }

    public function get(string $url, array $header = [], $timeout = 2): string
    {
        $client = $this->clientFactory->create([]);
        $response = $client->get($url, [
            'headers' => $header,
            'timeout' => $timeout,
        ]);
        return $response->getBody()->getContents();
    }
}
